/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Clases.Consulta;
import Clases.Producto;
import Clases.Proveedor;
import java.util.Calendar;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author server-data
 */
public class AgregarEntrada extends javax.swing.JFrame {

    /**
     * Creates new form AgregarOrden
     */
    public AgregarEntrada() {
        initComponents();
        DefaultComboBoxModel cm = (DefaultComboBoxModel) listProveedor.getModel();
        try {
            if (Consulta.getProveedores() != null) {
                cm.addElement("Seleccione un Proveedor");
                for (Proveedor proveedor : Consulta.getProveedores()) {
                    cm.addElement(proveedor.getNombre());
                }
            }
        } catch (Exception e) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menu_tabla = new javax.swing.JPopupMenu();
        pop_eliminar_fila = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jPanel1 = new javax.swing.JPanel();
        listProductos = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        btnAgregar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaProductos = new javax.swing.JTable();
        btnGuardar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        listProveedor = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtCodigoOrden = new javax.swing.JTextField();

        pop_eliminar_fila.setText("Eliminar");
        pop_eliminar_fila.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pop_eliminar_filaActionPerformed(evt);
            }
        });
        menu_tabla.add(pop_eliminar_fila);
        menu_tabla.add(jSeparator1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar Orden");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalles"));

        jLabel1.setText("Producto");

        jLabel3.setText("Cantidad");

        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtCantidad)
                        .addComponent(listProductos, 0, 178, Short.MAX_VALUE))
                    .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(listProductos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        tablaProductos.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tablaProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código Del Producto", "Descripción", "Cantidad Entrante"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaProductos.setComponentPopupMenu(menu_tabla);
        tablaProductos.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tablaProductos);
        tablaProductos.getColumnModel().getColumn(0).setResizable(false);
        tablaProductos.getColumnModel().getColumn(1).setResizable(false);
        tablaProductos.getColumnModel().getColumn(2).setResizable(false);

        btnGuardar.setText("Registrar Entrada");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Cabecera"));

        listProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listProveedorActionPerformed(evt);
            }
        });

        jLabel2.setText("Proveedor");

        jLabel4.setText("Orden");
        jLabel4.setToolTipText("");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCodigoOrden)
                    .addComponent(listProveedor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(35, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(listProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCodigoOrden, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(42, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(46, 46, 46)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(42, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        // TODO add your handling code here:
        String[] fila = new String[3];
        fila[0] = Consulta.getCodigoProducto((String) listProductos.getSelectedItem());
        fila[1] = (String) listProductos.getSelectedItem();
        fila[2] = txtCantidad.getText();
        DefaultTableModel modelo = (DefaultTableModel) tablaProductos.getModel();
        modelo.addRow(fila);
        tablaProductos.setModel(modelo);
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void pop_eliminar_filaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pop_eliminar_filaActionPerformed
        // TODO add your handling code here:
        try {
            int fila = tablaProductos.getSelectedRow();
            DefaultTableModel modelo = (DefaultTableModel) tablaProductos.getModel();
            modelo.removeRow(fila);
            tablaProductos.setModel(modelo);
        } catch (Exception e) {
            javax.swing.JOptionPane.showMessageDialog(null, "No ha seleccionado ninguna fila", "Seleccione una fila", 1);
        }
    }//GEN-LAST:event_pop_eliminar_filaActionPerformed

    private void listProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listProveedorActionPerformed
        // TODO add your handling code here:
        String proveedor = (String) listProveedor.getSelectedItem();
        DefaultComboBoxModel cm = (DefaultComboBoxModel) listProductos.getModel();
        cm.removeAllElements();
        try {
            if (Consulta.getProveedores() != null) {
                cm.addElement("Seleccione un Producto");
                for (Producto producto : Consulta.getProductos(proveedor)) {
                    cm.addElement(producto.getDescripcionProducto());
                }
            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_listProveedorActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed

        String nombreProveedor = (String) listProveedor.getSelectedItem();
        String codigoProveedor = Consulta.getCodigoProveedor(nombreProveedor);
        Calendar calendar = Calendar.getInstance();
        String date = String.valueOf(calendar.DATE).concat("/").concat(String.valueOf(calendar.MONTH).concat("/").concat(String.valueOf(calendar.YEAR)));
        String codigoEntrada;
        String codigoOrden = txtCodigoOrden.getText();
        if (tablaProductos.getRowCount() > 0) {
            if (codigoOrden.length() > 0) {
                Consulta.agregarEntrada(codigoOrden, date);
                codigoEntrada = Consulta.listarEntrada().get(Consulta.listarEntrada().size() - 1).getCodigoEntrada();
                for (int i = 0; i < tablaProductos.getRowCount(); i++) {
                    String codigoProducto = tablaProductos.getValueAt(i, 0).toString();
                    String cantidad = tablaProductos.getValueAt(i, 2).toString();
                    Consulta.agregarDetalleEntrada(codigoEntrada, codigoProveedor, codigoProducto, Integer.valueOf(cantidad));
                }
                javax.swing.JOptionPane.showMessageDialog(null, "Guardado !");
                DefaultComboBoxModel cm = (DefaultComboBoxModel)listProductos.getModel();
                cm.removeAllElements();
                txtCantidad.setText("");
                txtCodigoOrden.setText("");
                listProductos.setSelectedIndex(0);
                listProveedor.setSelectedIndex(0);
            }else{
                javax.swing.JOptionPane.showMessageDialog(null, "No ha Digitado el Código de la Entrada", "Error",2);
            }
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "No hay nada que guardar", "Ingrese datos", 1);
        }
        txtCantidad.setText("");
        listProductos.setSelectedIndex(0);
        listProveedor.setSelectedIndex(0);

    }//GEN-LAST:event_btnGuardarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JComboBox listProductos;
    private javax.swing.JComboBox listProveedor;
    private javax.swing.JPopupMenu menu_tabla;
    private javax.swing.JMenuItem pop_eliminar_fila;
    private javax.swing.JTable tablaProductos;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtCodigoOrden;
    // End of variables declaration//GEN-END:variables
}
